/*
 *  SSDiamondLabelPanelController.m
 *  MESLib
 * 
 * Copyright 2003-2009 Starline Software Ltd
 * 
 * This file is part of MESLib.
 * 
 * MESLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MESLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with MESLib. If not, see <http://www.gnu.org/licenses/>.
 */
//  Created by Tobias Haughton on Thu Oct 02 2003.
//

#import "SSDiamondLabelPanelController.h"
#import "SSCatalystDocument.h"

@implementation SSDiamondLabelPanelController

+ (id)sharedDiamondLabelPanelController {
   static SSDiamondLabelPanelController *sharedDiamondLabelPanelController = nil;

   if(!sharedDiamondLabelPanelController) {
      sharedDiamondLabelPanelController = [[SSDiamondLabelPanelController allocWithZone:[self zone]] init];
   }
   
   return sharedDiamondLabelPanelController;
}

- (id)init {
   self = [super initWithWindowNibName:@"DiamondLabelPanel"];
   if (self) {
      [self setWindowFrameAutosaveName:@"DiamondLabelPanel"];
   }
   return self;
}

- (void)awakeFromNib
{
   //set the other control values
   [self setControlValues];
}

- (void)setMainWindow:(NSWindow *)mainWindow {
   needsUpdate = YES;
   [self updateDefaultCheckboxStates];
}

- (void)windowDidUpdate:(NSNotification *)notification {
   //this is called each time the window updates
   if(needsUpdate) {
      [self setControlValues];
      needsUpdate = NO;
   }
}

- (void)setControlValues
{
   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue1]) {
      [diamondUD1Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue1]];
   } else {
      [diamondUD1Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue2]) {
      [diamondUD2Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue2]];
   } else {
      [diamondUD2Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue3]) {
      [diamondUD3Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue3]];
   } else {
      [diamondUD3Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue4]) {
      [diamondUD4Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue4]];
   } else {
      [diamondUD4Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue5]) {
      [diamondUD5Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue5]];
   } else {
      [diamondUD5Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue6]) {
      [diamondUD6Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue6]];
   } else {
      [diamondUD6Field setStringValue:[NSString string]];
   }
   
   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue1]) {
      [optionUD1Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue1]];
   } else {
      [optionUD1Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue2]) {
      [optionUD2Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue2]];
   } else {
      [optionUD2Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue3]) {
      [optionUD3Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue3]];
   } else {
      [optionUD3Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue4]) {
      [optionUD4Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue4]];
   } else {
      [optionUD4Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue5]) {
      [optionUD5Field setObjectValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue5]];
   } else {
      [optionUD5Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue1]) {
      [tradeoffUD1Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue1]];
   } else {
      [tradeoffUD1Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue2]) {
      [tradeoffUD2Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue2]];
   } else {
      [tradeoffUD2Field setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue3]) {
      [tradeoffUD3Field setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue3]];
   } else {
      [tradeoffUD3Field setStringValue:[NSString string]];
   }
}

- (IBAction)setDefaultValue:(id)sender
{
   //save the values to the user defaults
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   if([sender state]) {
      switch([sender tag]) {
         case 0:[defaults setObject:[diamondUD1Field stringValue] forKey:SSDefaultDiamondUDValue1Key];
            break;
         case 1:[defaults setObject:[diamondUD2Field stringValue] forKey:SSDefaultDiamondUDValue2Key];
            break;
         case 2:[defaults setObject:[diamondUD3Field stringValue] forKey:SSDefaultDiamondUDValue3Key];
            break;
         case 3:[defaults setObject:[diamondUD4Field stringValue] forKey:SSDefaultDiamondUDValue4Key];
            break;
         case 4:[defaults setObject:[diamondUD5Field stringValue] forKey:SSDefaultDiamondUDValue5Key];
            break;
         case 5:[defaults setObject:[diamondUD6Field stringValue] forKey:SSDefaultDiamondUDValue6Key];
            break;
         case 6:[defaults setObject:[optionUD1Field stringValue] forKey:SSDefaultDiamondOptionUDValue1Key];
            break;
         case 7:[defaults setObject:[optionUD2Field stringValue] forKey:SSDefaultDiamondOptionUDValue2Key];
            break;
         case 8:[defaults setObject:[optionUD3Field stringValue] forKey:SSDefaultDiamondOptionUDValue3Key];
            break;
         case 9:[defaults setObject:[optionUD4Field stringValue] forKey:SSDefaultDiamondOptionUDValue4Key];
            break;
         case 10:[defaults setObject:[optionUD5Field stringValue] forKey:SSDefaultDiamondOptionUDValue5Key];
            break;
         case 11:[defaults setObject:[tradeoffUD1Field stringValue] forKey:SSDefaultDiamondTradeoffUDValue1Key];
            break;
         case 12:[defaults setObject:[tradeoffUD2Field stringValue] forKey:SSDefaultDiamondTradeoffUDValue2Key];
            break;
         case 13:[defaults setObject:[tradeoffUD3Field stringValue] forKey:SSDefaultDiamondTradeoffUDValue3Key];
            break;
         default:
            break;
      }      
   } 
}

- (void)controlTextDidEndEditing:(NSNotification *)note
{
   //called when a text field stops editing
   switch([[note object] tag]) {
      case 0:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue1]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondUDValue1:[[note object] stringValue]];
         }
         break;
      case 1:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue2]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondUDValue2:[[note object] stringValue]];
         }
         break;
      case 2:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue3]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondUDValue3:[[note object] stringValue]];
         }
         break;
      case 3:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue4]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondUDValue4:[[note object] stringValue]];
         }
         break;
      case 4:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue5]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondUDValue5:[[note object] stringValue]];
         }
         break;
      case 5:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue6]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondUDValue6:[[note object] stringValue]];
         }
         break;
      case 6:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue1]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondOptionUDValue1:[[note object] stringValue]];
         }
         break;
      case 7:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue2]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondOptionUDValue2:[[note object] stringValue]];
         }
         break;
      case 8:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue3]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondOptionUDValue3:[[note object] stringValue]];
         }
         break;
      case 9:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue4]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondOptionUDValue4:[[note object] stringValue]];
         }
         break;
      case 10:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue5]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondOptionUDValue5:[[note object] stringValue]];
         }
         break;
      case 11:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue1]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondTradeoffUDValue1:[[note object] stringValue]];
         }
         break;
      case 12:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue2]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondTradeoffUDValue2:[[note object] stringValue]];
         }
         break;
      case 13:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue3]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setDiamondTradeoffUDValue3:[[note object] stringValue]];
         }
         break;
      default:
         break;
   }
}

- (void)controlTextDidChange:(NSNotification *)note
{
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

   //update the checkbox state
   switch([[note object] tag]) {
      case 0:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue1Key]]) { 
            [diamondUD1DefaultCheckbox setState:NSOnState];
         } else {
            [diamondUD1DefaultCheckbox setState:NSOffState];
         }
         break;
      case 1:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue2Key]]) {
            [diamondUD2DefaultCheckbox setState:NSOnState];
         } else {
            [diamondUD2DefaultCheckbox setState:NSOffState];
         }
         break;
      case 2:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue3Key]]) {
            [diamondUD3DefaultCheckbox setState:NSOnState];
         } else {
            [diamondUD3DefaultCheckbox setState:NSOffState];
         }
         break;
      case 3:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue4Key]]) {
            [diamondUD4DefaultCheckbox setState:NSOnState];
         } else {
            [diamondUD4DefaultCheckbox setState:NSOffState];
         }
         break;
      case 4:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue5Key]]) { 
            [diamondUD5DefaultCheckbox setState:NSOnState];
         } else {
            [diamondUD5DefaultCheckbox setState:NSOffState];
         }
         break;
      case 5:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue6Key]]) {
            [diamondUD6DefaultCheckbox setState:NSOnState];
         } else {
            [diamondUD6DefaultCheckbox setState:NSOffState];
         }
         break;
      case 6:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondOptionUDValue1Key]]) {
            [optionUD1DefaultCheckbox setState:NSOnState];
         } else {
            [optionUD1DefaultCheckbox setState:NSOffState];
         }
         break;
      case 7:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondOptionUDValue2Key]]) {
            [optionUD2DefaultCheckbox setState:NSOnState];
         } else {
            [optionUD2DefaultCheckbox setState:NSOffState];
         }
         break;
      case 8:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondOptionUDValue3Key]]) {
            [optionUD3DefaultCheckbox setState:NSOnState];
         } else {
            [optionUD3DefaultCheckbox setState:NSOffState];
         }
         break;
      case 9:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondOptionUDValue4Key]]) {
            [optionUD4DefaultCheckbox setState:NSOnState];
         } else {
            [optionUD4DefaultCheckbox setState:NSOffState];
         }
         break;
      case 10:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondOptionUDValue5Key]]) {
            [optionUD5DefaultCheckbox setState:NSOnState];
         } else {
            [optionUD5DefaultCheckbox setState:NSOffState];
         }
         break;
      case 11:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondTradeoffUDValue1Key]]) {
            [tradeoffUD1DefaultCheckbox setState:NSOnState];
         } else {
            [tradeoffUD1DefaultCheckbox setState:NSOffState];
         }
         break;
      case 12:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondTradeoffUDValue2Key]]) {
            [tradeoffUD2DefaultCheckbox setState:NSOnState];
         } else {
            [tradeoffUD2DefaultCheckbox setState:NSOffState];
         }
         break;
      case 13:
         if([[[note object] stringValue] isEqualToString:[defaults stringForKey:SSDefaultDiamondTradeoffUDValue3Key]]) {
            [tradeoffUD3DefaultCheckbox setState:NSOnState];
         } else {
            [tradeoffUD3DefaultCheckbox setState:NSOffState];
         }
         break;
      default:
         break;
   }   
}

- (void)updateDefaultCheckboxStates
{
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

   if([[NSDocumentController sharedDocumentController] currentDocument]) {
      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue1] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue1Key]]) {
         [diamondUD1DefaultCheckbox setState:NSOnState];
      } else {
         [diamondUD1DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue2] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue2Key]]) {
         [diamondUD2DefaultCheckbox setState:NSOnState];
      } else {
         [diamondUD2DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue3] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue3Key]]) {
         [diamondUD3DefaultCheckbox setState:NSOnState];
      } else {
         [diamondUD3DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue4] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue4Key]]) {
         [diamondUD4DefaultCheckbox setState:NSOnState];
      } else {
         [diamondUD4DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue5] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue5Key]]) {
         [diamondUD5DefaultCheckbox setState:NSOnState];
      } else {
         [diamondUD5DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondUDValue6] isEqualToString:[defaults stringForKey:SSDefaultDiamondUDValue6Key]]) {
         [diamondUD6DefaultCheckbox setState:NSOnState];
      } else {
         [diamondUD6DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue1] isEqualToString:[defaults stringForKey:SSDefaultDiamondOptionUDValue1Key]]) {
         [optionUD1DefaultCheckbox setState:NSOnState];
      } else {
         [optionUD1DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue2] isEqualToString:[defaults stringForKey:SSDefaultDiamondOptionUDValue2Key]]) {
         [optionUD2DefaultCheckbox setState:NSOnState];
      } else {
         [optionUD2DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue3] isEqualToString:[defaults stringForKey:SSDefaultDiamondOptionUDValue3Key]]) {
         [optionUD3DefaultCheckbox setState:NSOnState];
      } else {
         [optionUD3DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue4] isEqualToString:[defaults stringForKey:SSDefaultDiamondOptionUDValue4Key]]) {
         [optionUD4DefaultCheckbox setState:NSOnState];
      } else {
         [optionUD4DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondOptionUDValue5] isEqualToString:[defaults stringForKey:SSDefaultDiamondOptionUDValue5Key]]) {
         [optionUD5DefaultCheckbox setState:NSOnState];
      } else {
         [optionUD5DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue1] isEqualToString:[defaults stringForKey:SSDefaultDiamondTradeoffUDValue1Key]]) {
         [tradeoffUD1DefaultCheckbox setState:NSOnState];
      } else {
         [tradeoffUD1DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue2] isEqualToString:[defaults stringForKey:SSDefaultDiamondTradeoffUDValue2Key]]) {
         [tradeoffUD2DefaultCheckbox setState:NSOnState];
      } else {
         [tradeoffUD2DefaultCheckbox setState:NSOffState];
      }

      if([[[[NSDocumentController sharedDocumentController] currentDocument] diamondTradeoffUDValue3] isEqualToString:[defaults stringForKey:SSDefaultDiamondTradeoffUDValue3Key]]) {
         [tradeoffUD3DefaultCheckbox setState:NSOnState];
      } else {
         [tradeoffUD3DefaultCheckbox setState:NSOffState];
      }
   }
}

- (void)dealloc
{
   [[NSNotificationCenter defaultCenter] removeObserver:self];
   [super dealloc];
}

@end
